// package com.tiger.cloud.filter;
//   
//  import java.security.messagedigest;
//  import java.util.calendar;
//  import java.util.date;
//    import java.util.hashmap;
//  import java.util.map;
//  import java.util.map.entry;
//  import java.util.concurrent.executors;
//  import java.util.concurrent.scheduledexecutorservice;
//  import java.util.concurrent.timeunit;
//  
//  import org.apache.log4j.logger;
// 
// 
// 
// public class tokenutil {
//  
//      private static final int interval = 7;// token过期时间间隔 天
//      private static final string yan = "testmrf1$789787aadfjkds//*-+'[]jfeu;384785*^*&%^%$%";// 加盐
//      private static final int hour = 3;// 检查token过期线程执行时间 时
//      
//      private static logger logger = logger.getlogger("visit");
//  
//      private static map<integer, token> tokenmap = new hashmap<integer, token>();
//      private static tokenutil tokenutil = null;
//      static scheduledexecutorservice scheduler =executors.newsinglethreadscheduledexecutor(); 
//  
//      static {
//          logger.info("\n===============进入tokenutil静态代码块==================");
//          listentask();
//     }
//      
//  
//      public static tokenutil gettokenutil() {
//          if (tokenutil == null) {
//             syninit();
//         }
//  
//          return tokenutil;
//     }
// 
//     private static synchronized void syninit() {
//          if (tokenutil == null) {
//             tokenutil = new tokenutil();
//         }
//     }
// 
//     public tokenutil() {
//      }
//      public static map<integer, token> gettokenmap() {
//          return tokenmap;
//     }
// 
//     /**
//      * 产生一个token
//        */
//     public static token generatetoken(string uniq,int id) {
//          token token = new token(md5(system.currenttimemillis()+yan+uniq+id), system.currenttimemillis());
//        synchronized (tokenmap) {
//              tokenmap.put(id, token);
//        }
//          return token;
//     }
// 
//   
//       /**
//        * @title: removetoken
//        * @description: 去除token
//       * @param @param nonce
//        * @param @return 参数
//        * @return boolean 返回类型
//       */
//       public static boolean removetoken(int id) {
//           synchronized (tokenmap) {
//             tokenmap.remove(id);
//             logger.info(tokenmap.get(id) == null ? "\n=========已注销========": "\n++++++++注销失败+++++++++++++++");
//          }
//         return true;
//      }
//  
//       /**
// 86      * @title: volidatetoken
// 87      * @description: 校验token
// 88      * @param @param signature
// 89      * @param @param nonce
// 90      * @param @return 参数
// 91      * @return boolean 返回类型
// 92      */
// 93     public static boolean volidatetoken(string signature, int id) {
// 94         boolean flag = false;
// 95         token token = (token) tokenmap.get(id);
// 96         if (token != null && token.getsignature().equals(signature)) {
// 97             logger.info("\n=====已在线=======");
// 98             flag = true;
// 99         }
//100 
//101         return flag;
//102     }
//103     
//104     /**
//105      * 
//106      * @title: md5
//107      * @description: 加密
//108      * @param @param s
//109      * @param @return 参数
//110      * @return string 返回类型
//111      */
//112     public final static string md5(string s) {
//113         try {
//114             byte[] btinput = s.getbytes();
//115             // 获得md5摘要算法的 messagedigest 对象
//116             messagedigest mdinst = messagedigest.getinstance("md5");
//117             // 使用指定的字节更新摘要
//118             mdinst.update(btinput);
//119             // 获得密文
//120             return byte2hex(mdinst.digest());
//121         } catch (exception e) {
//122             e.printstacktrace();
//123             return null;
//124         }
//125     }
//126 
//127     /**
//128      * 将字节数组转换成16进制字符串
//129      * @param b
//130      * @return
//131      */
//132     private static string byte2hex(byte[] b) {
//133         stringbuilder sbdes = new stringbuilder();
//134         string tmp = null;
//135         for (int i = 0; i < b.length; i++) {
//136             tmp = (integer.tohexstring(b[i] & 0xff));
//137             if (tmp.length() == 1) {
//138                 sbdes.append("0");
//139             }
//140             sbdes.append(tmp);
//141         }
//142         return sbdes.tostring();
//143     }
//144     
//145     /**
//146     * @title: listentask 
//147     * @description: 定时执行token过期清除任务
//148     * @param     参数
//149     * @return void    返回类型
//150      */
//151     public static void listentask(){
//152         calendar calendar = calendar.getinstance();
//153         int year = calendar.get(calendar.year);
//154         int month = calendar.get(calendar.month);
//155         int day = calendar.get(calendar.day_of_month);
//156         //定制每天的hour点，从明天开始
//157         calendar.set(year, month, day+1, hour, 0, 0);
//158        // calendar.set(year, month, day, 17, 11, 40);
//159         date date = calendar.gettime();
//160         
//161         scheduler.scheduleatfixedrate( new listentoken(), (date.gettime()-system.currenttimemillis())/1000, 60*60*24, timeunit.seconds);
//162     }
//163     
//164     
//165 
//166     /**
//167      * @classname: listentoken
//168      * @description: 监听token过期线程runnable实现
//169      * @author mrf
//170      * @date 2015-10-21 下午02:22:24
//171      * 
//172      */
//173     static class listentoken implements runnable {
//174         public listentoken() {
//175             super();
//176         }
//177 
//178         public void run() {
//179             logger.info("\n**************************执行监听token列表****************************");
//180             try {
//181                 synchronized (tokenmap) {
//182                     for (int i = 0; i < 5; i++) {
//183                         if (tokenmap != null && !tokenmap.isempty()) {
//184                             for (entry<integer, token> entry : tokenmap.entryset()) {
//185                                 token token = (token) entry.getvalue();
//186                                 logger.info("\n==============已登录用户有："+entry + "=====================");
//187 //                            try {thread.sleep(1000);} catch (interruptedexception e) {e.printstacktrace();}
//188                                 int interval = (int) ((system.currenttimemillis() - token.gettimestamp()) / 1000 / 60 / 60 / 24);
//189                                 if (interval > interval) {
//190                                     tokenmap.remove(entry.getkey());
//191                                     logger.info("\n==============移除token：" + entry+ "=====================");
//192                                 }
//193 
//194                             }
//195                         }
//196                     }
//197                     
//198                 }
//199             } catch (exception e) {
//200                 logger.error("token监听线程错误："+e.getmessage());
//201                 e.printstacktrace();
//202             }
//203         }
//204     }
//205     
//206      
//207 
//208     public static void main(string[] args) {
//209         system.out.println(generatetoken( "s",1));
//210         system.out.println(generatetoken( "q",1));
//211         system.out.println(generatetoken( "s3",2));
//212         system.out.println(generatetoken( "s4",3));
//213         system.out.println(removetoken(3));
//214         system.out.println(gettokenmap());
//215     }
//216 
//217 }
//218 
//219  